package com.maddox.maddoxppjoke.ui.home;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.paging.DataSource;
import androidx.paging.ItemKeyedDataSource;

import com.alibaba.fastjson.TypeReference;
import com.maddox.libnetwork.ApiResponse;
import com.maddox.libnetwork.ApiService;
import com.maddox.libnetwork.JsonCallback;
import com.maddox.libnetwork.Request;
import com.maddox.maddoxppjoke.AbsViewModel;
import com.maddox.maddoxppjoke.model.Feed;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

//首页数据加载
public class HomeViewModel extends AbsViewModel<Feed> {

    private volatile boolean withCache = true;

    @Override
    public DataSource createDataSource() {
        return itemKeyedDataSource;
    }

    ItemKeyedDataSource<Integer, Feed> itemKeyedDataSource = new ItemKeyedDataSource<Integer, Feed>() {


        @Override
        public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Feed> callback) {
            //加载初始化数据
            loadData(0, callback);
            //只缓存第一次请求的数据或者下拉刷新,上拉加载的数据不再进行缓存
            withCache = false;

        }

        @Override
        public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Feed> callback) {
            //加载分页数据
            loadData(params.key, callback);
        }

        @Override
        public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Feed> callback) {
            //向前加载数据
            callback.onResult(Collections.emptyList());
        }

        @NonNull
        @Override
        public Integer getKey(@NonNull Feed item) {
            return item.id;
        }
    };

    private void loadData(int key, ItemKeyedDataSource.LoadCallback<Feed> callback) {
        //  feeds/queryHotFeedsList  查询帖子列表数据
        Request request = ApiService.get("/feeds/queryHotFeedsList")
                .addParam("feedId", key)
                .addParam("feedType", "all")
                .addParam("pageCount", 10)
                .addParam("userId", 0)
                .responseType(new TypeReference<ArrayList<Feed>>() {
                }.getType());
        //先去读取缓存
        if (withCache) {
            request.setCacheStrategy(Request.CACHE_ONLY);
            request.execute(new JsonCallback<List<Feed>>() {
                @Override
                public void cacheSuccess(ApiResponse<List<Feed>> response) {
                    Log.d("Maddox", response.body==null?"无数据111":response.body.toString());
                }
            });
        }
        Request netRequest = null;
        try {
            //进行网络请求
            netRequest = withCache ? request.clone() : request;
            //下拉刷新=0
            netRequest.setCacheStrategy(key == 0 ? Request.NET_CACHE : Request.NET_ONLY);
            ApiResponse<List<Feed>> response = netRequest.execute();
            Log.d("Maddox", response.body==null?"无数据222":response.body.toString());
            List<Feed> data = response.body == null ? Collections.emptyList() : response.body;
            callback.onResult(data);
            if (key > 0) {
                getBoundaryPageData().postValue(data.size() > 0);
            }


        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }


    }

}